package effects.post
{
	import flash.display.DisplayObject;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.display.BlendMode;
	import flash.geom.ColorTransform;
	import evo.engine.AbstractPostProcessor;
	
	/**
	 * Effectively scales down the effect quality and resolution by providing smaller buffer
	 * for effects running under this effect.
	 * 
	 * Downgrade effect is excellent if effect runs relatively fast and/or is shown only
	 * with videoscreens (they can't see the pixelation)
	 * 
	 * Basically you could also use downgrade as simple pixellate effect too with high
	 * scaling values.
	 */ 
	public class DowngradeEffect extends AbstractPostProcessor
	{	
		private var scaleMatrix:Matrix;

		private var scale:Number = 1;
		private var div:Number;
		private var blendMode:String;
		private var smooth:Boolean = false;
		private var forceTransparent:Boolean;
		
		public override function init(params:Object=null):void
		{	
			if(params.scale)
			{
				div = scale = params.scale;
				scaleMatrix = new Matrix();
				
				if(params.smooth)
				{
					smooth = params.smooth;	
				}
				if(params.scaley)
				{
					div = params.scaley;
				}
				if(params.forceTransparent != null)
				{
					forceTransparent = true;				
				}
				scaleMatrix.scale(scale,div);
			}
			if(params.blendMode)
				blendMode = params.blendMode;
			
			super.init(params);
		}
		
		public override function set output(buffer:BitmapData):void
		{
			super.output = buffer;
			
			var transp:Boolean = output.transparent || forceTransparent;
			input = new BitmapData(output.width/scale, output.height/div, transp, 0);
		}
			
		public override function play(dt:Number):void
		{
				_input.lock();
				_input.fillRect(_input.rect,0);

				super.play(dt);
				_input.unlock();
				buffer.lock();							
				buffer.draw(_input,scaleMatrix, null, blendMode,null, smooth);
				buffer.unlock();
		};
	}
}